package ziweiUtil

func GetGanNum(str string) int {

	for K := range GanMap {
		if K == str {
			num := GanMap[K]
			return num
		}
	}
	return 0
}
func GetZhiNum(str string) int {

	for K := range ZhiMap {
		if K == str {
			num := ZhiMap[K]
			return num
		}
	}
	return 0
}

type Infos struct {
	Name  string
	Index int
}

//获取五行局
func GetWuXingJuName(f int) Infos {
	var out Infos

	for k, v := range WuXingJuName {
		if k == f {
			out = Infos{
				Name:  v,
				Index: k,
			}

		}
	}
	return out
}

func WXJ(f int) []string {
	var out []string

	for k, v := range WuXingJuName {
		if k == f {
			out = append(out, v)
		}

	}
	return out

}

// GetWuXingJuKey 输入字符串，去遍历WuxingJu map，返回对应的key
func GetWuXingJuKey(str string) int {

	for k, v := range WuXingJu {
		//	遍历map，如果value等于输入的字符串，返回key
		//判断输入的字符串是否包含在value数组中
		if isInclude(v, str) {
			return k
		}
	}
	return 0
}
func isInclude(arr []string, str string) bool {
	for _, v := range arr {
		if v == str {
			return true
		}
	}
	return false
}

func GetGanWuXing(gan int) string {
	switch gan {
	case 9, 10:
		{
			return "金"
		}
	case 1, 2:
		{
			return "木"
		}
	case 3, 4:
		{
			return "水"
		}
	case 5, 6:
		{
			return "火"
		}
	case 7, 8:
		{
			return "土"
		}
	}
	return ""
}
func GetZhiWuXing(zhi int) string {
	switch zhi {
	case 3, 4:
		{
			return "木"
		}
	case 9, 10:
		{
			return "火"
		}
	case 2, 5, 8, 11:
		{
			return "土"
		}
	case 6, 7:
		{
			return "金"
		}
	case 12, 1:
		{
			return "水"
		}

	}
	return ""
}

//定位流年盘宫位
func GetLiuNianPan() map[string]int {

	T1 := 4

	MingPan := make(map[string]int)
	MingPan["命宫"] = T1
	MingPan["兄弟宫"] = T1 - 1
	MingPan["夫妻宫"] = T1 - 2
	MingPan["子女宫"] = T1 - 3
	MingPan["财帛宫"] = T1 - 4
	MingPan["疾厄宫"] = T1 - 5
	MingPan["迁移宫"] = T1 - 6
	MingPan["交友宫"] = T1 - 7
	MingPan["事业宫"] = T1 - 8
	MingPan["田宅宫"] = T1 - 9
	MingPan["福德宫"] = T1 - 10
	MingPan["父母宫"] = T1 - 11

	for k, v := range MingPan {
		if v < 0 || v == 0 {
			v = v + 12
		} else if v > 12 {
			v = v % 12
		}

		MingPan[k] = v
	}

	return MingPan
}

//定位流月盘宫位
func GetLiuYuePan(m, h int) map[int]int {
	y3 := 4
	MingPan := make(map[int]int)
	MingPan[1] = y3 - m + h
	MingPan[2] = y3 - m + h + 1
	MingPan[3] = y3 - m + h + 2
	MingPan[4] = y3 - m + h + 3
	MingPan[5] = y3 - m + h + 4
	MingPan[6] = y3 - m + h + 5
	MingPan[7] = y3 - m + h + 6
	MingPan[8] = y3 - m + h + 7
	MingPan[9] = y3 - m + h + 8
	MingPan[10] = y3 - m + h + 9
	MingPan[11] = y3 - m + h + 10
	MingPan[12] = y3 - m + h + 11
	for k, v := range MingPan {
		if v < 0 || v == 0 {
			v = v + 12
		} else if v > 12 {
			v = v % 12
		}

		MingPan[k] = v
	}
	return MingPan
}

// GetBenMingPan //定位本命盘-宫位：命宫
func GetBenMingPan(M, H int) map[string]int {

	//fmt.Println("定位命宫", "M:", M, "H:", H)
	//M为月支，H为时支
	//计算本命盘 3+M-H
	var T1 int
	T1 = 3 + M - H
	if T1 < 0 {
		T1 = T1 + 12
	}

	MingPan := make(map[string]int)
	MingPan["命宫"] = T1
	MingPan["兄弟宫"] = T1 - 1
	MingPan["夫妻宫"] = T1 - 2
	MingPan["子女宫"] = T1 - 3
	MingPan["财帛宫"] = T1 - 4
	MingPan["疾厄宫"] = T1 - 5
	MingPan["迁移宫"] = T1 - 6
	MingPan["交友宫"] = T1 - 7
	MingPan["事业宫"] = T1 - 8
	MingPan["田宅宫"] = T1 - 9
	MingPan["福德宫"] = T1 - 10
	MingPan["父母宫"] = T1 - 11

	for k, v := range MingPan {
		if v < 0 || v == 0 {
			v = v + 12
		} else if v > 12 {
			v = v % 12
		}

		MingPan[k] = v
	}

	return MingPan
}

// GetStarNum 本命盘紫薇星定位，用D(日）和F（五行宫）来计算
func GetStarNum(D, F int) int {
	var a, b int

	for a = 1; a < 100; a++ {
		for b = 1; b < 100; b++ {
			if D+b-F*a == 0 {
				//fmt.Printf("a=%d, b=%d\n", a, b)
				return GetStar(a, b)
				//break
			}
		}
	}
	return 0
}

// GetStar 定位紫薇星
func GetStar(a, b int) int {
	var num int
	if IsEven(b) {
		num = 3 + a + b
		//return num
	} else {
		num = 3 + a - b
		//if num < 0 {
		//	num = num + 12
		//}
		//return num
	}
	if num < 0 || num == 0 {
		num = num + 12
	} else if num > 12 {
		num = num % 12
	}

	return num
}

func IsEven(num int) bool {
	if num%2 == 0 {
		return true
	} else {
		return false
	}
}

// GetTianFuStar 计算天府星
func GetTianFuStar(T1 int) int {
	var T2 int

	if T1 >= 6 {
		T2 = 18 - T1
	} else {
		T2 = 6 - T1
		/*	if T2 < 0 {
			T2 = T2 + 12
		}*/
	}
	if T2 < 0 || T2 == 0 {
		T2 = T2 + 12
	} else if T2 > 12 {
		T2 = T2 % 12
	}

	return T2
}

// GetStarPan 计算星盘
func GetStarPan(T1, T2 int) map[string]int {
	stars := make(map[string]int)

	stars["紫微"] = T1
	stars["天机"] = T1 - 1  //天机
	stars["太阳"] = T1 - 3  //太阳
	stars["武曲"] = T1 - 4  //武曲
	stars["天同"] = T1 - 5  //天同
	stars["廉贞"] = T1 - 8  //廉贞
	stars["天府"] = T2      //天府
	stars["太阴"] = T2 + 1  //太阴
	stars["贪狼"] = T2 + 2  //贪狼
	stars["巨门"] = T2 + 3  //巨门
	stars["天相"] = T2 + 4  //天相
	stars["天梁"] = T2 + 5  //天梁
	stars["七杀"] = T2 + 6  //七杀
	stars["破军"] = T2 + 10 //破军

	for k, v := range stars {
		if v < 0 || v == 0 {
			v = v + 12
		} else if v > 12 {
			v = v % 12
		}

		stars[k] = v
	}

	return stars

}

// GetMingGongStar 计算命宫主星输入命宫的数字,和星盘，返回命宫主星的名称
func GetMingGongStar(n1 int, star map[string]int) string {
	var key string
	//	遍历star 看与n1相等的value的key值
	for k, v := range star {
		if v == n1 {
			key = k
			break
		} else {
			switch {
			case n1 <= 6:
				if v == n1+6 {
					key = k
					break
				}
			case n1 > 6:
				if v == n1-6 {
					key = k
					break
				}
			}
		}
	}

	return key
}
func GetMingGongKey(s string, star map[string]int) PlaceInfo {
	var v PlaceInfo
	//	遍历star 看与n1相等的value的key值
	for k := range star {
		if k == s {
			v.Index = star[k]
			v.Name = k
			break
		}
	}

	return v
}
